using HKB.Common;
using HKB.Data.Model;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
namespace HKB.Data.Dal
{
	public class UserGiftCardDal
	{
		public bool Exists(string CardNumber)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select count(1) from MBF_UserGiftCard");
			stringBuilder.Append(" where ");
			stringBuilder.Append("CardNumber = @CardNumber ");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@CardNumber", SqlDbType.VarChar, 50)
			};
			array[0].Value = CardNumber;
			return Conn.Exists(stringBuilder.ToString(), array);
		}
		public void Add(UserGiftCardModel model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("insert into MBF_UserGiftCard(");
			stringBuilder.Append("CardNumber,Password,FestivalName,FaceValue,Balance,CouponAmount,Picture,Title,BuyTime,BuyID,RecieveID,ReceiveEmail,LeaveWords");
			stringBuilder.Append(") values (");
			stringBuilder.Append("@CardNumber,@Password,@FestivalName,@FaceValue,@Balance,@CouponAmount,@Picture,@Title,@BuyTime,@BuyID,@RecieveID,@ReceiveEmail,@LeaveWords");
			stringBuilder.Append(") ");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@CardNumber", SqlDbType.VarChar, 50),
				new SqlParameter("@Password", SqlDbType.VarChar, 50),
				new SqlParameter("@FestivalName", SqlDbType.NVarChar, 50),
				new SqlParameter("@FaceValue", SqlDbType.Decimal, 9),
				new SqlParameter("@Balance", SqlDbType.Decimal, 9),
				new SqlParameter("@CouponAmount", SqlDbType.Decimal, 9),
				new SqlParameter("@Picture", SqlDbType.NVarChar, 50),
				new SqlParameter("@Title", SqlDbType.NVarChar, 50),
				new SqlParameter("@BuyTime", SqlDbType.DateTime),
				new SqlParameter("@BuyID", SqlDbType.Int, 4),
				new SqlParameter("@RecieveID", SqlDbType.Int, 4),
				new SqlParameter("@ReceiveEmail", SqlDbType.NVarChar, 50),
				new SqlParameter("@LeaveWords", SqlDbType.NVarChar, 300)
			};
			array[0].Value = model.CardNumber;
			array[1].Value = model.Password;
			array[2].Value = model.FestivalName;
			array[3].Value = model.FaceValue;
			array[4].Value = model.Balance;
			array[5].Value = model.CouponAmount;
			array[6].Value = model.Picture;
			array[7].Value = model.Title;
			array[8].Value = model.BuyTime;
			array[9].Value = model.BuyID;
			array[10].Value = model.RecieveID;
			array[11].Value = model.ReceiveEmail;
			array[12].Value = model.LeaveWords;
			Conn.ExecuteSql(stringBuilder.ToString(), array);
		}
		public bool Update(UserGiftCardModel model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("update MBF_UserGiftCard set ");
			stringBuilder.Append(" CardNumber = @CardNumber , ");
			stringBuilder.Append(" Password = @Password , ");
			stringBuilder.Append(" FestivalName = @FestivalName , ");
			stringBuilder.Append(" FaceValue = @FaceValue , ");
			stringBuilder.Append(" Balance = @Balance , ");
			stringBuilder.Append(" CouponAmount = @CouponAmount , ");
			stringBuilder.Append(" Picture = @Picture , ");
			stringBuilder.Append(" Title = @Title , ");
			stringBuilder.Append(" BuyTime = @BuyTime , ");
			stringBuilder.Append(" BuyID = @BuyID , ");
			stringBuilder.Append(" RecieveID = @RecieveID , ");
			stringBuilder.Append(" ReceiveEmail = @ReceiveEmail , ");
			stringBuilder.Append(" LeaveWords = @LeaveWords  ");
			stringBuilder.Append(" where CardNumber=@CardNumber  ");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@CardNumber", SqlDbType.VarChar, 50),
				new SqlParameter("@Password", SqlDbType.VarChar, 50),
				new SqlParameter("@FestivalName", SqlDbType.NVarChar, 50),
				new SqlParameter("@FaceValue", SqlDbType.Decimal, 9),
				new SqlParameter("@Balance", SqlDbType.Decimal, 9),
				new SqlParameter("@CouponAmount", SqlDbType.Decimal, 9),
				new SqlParameter("@Picture", SqlDbType.NVarChar, 50),
				new SqlParameter("@Title", SqlDbType.NVarChar, 50),
				new SqlParameter("@BuyTime", SqlDbType.DateTime),
				new SqlParameter("@BuyID", SqlDbType.Int, 4),
				new SqlParameter("@RecieveID", SqlDbType.Int, 4),
				new SqlParameter("@ReceiveEmail", SqlDbType.NVarChar, 50),
				new SqlParameter("@LeaveWords", SqlDbType.NVarChar, 300)
			};
			array[0].Value = model.CardNumber;
			array[1].Value = model.Password;
			array[2].Value = model.FestivalName;
			array[3].Value = model.FaceValue;
			array[4].Value = model.Balance;
			array[5].Value = model.CouponAmount;
			array[6].Value = model.Picture;
			array[7].Value = model.Title;
			array[8].Value = model.BuyTime;
			array[9].Value = model.BuyID;
			array[10].Value = model.RecieveID;
			array[11].Value = model.ReceiveEmail;
			array[12].Value = model.LeaveWords;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public bool Delete(string CardNumber)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("delete from MBF_UserGiftCard ");
			stringBuilder.Append(" where CardNumber=@CardNumber ");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@CardNumber", SqlDbType.VarChar, 50)
			};
			array[0].Value = CardNumber;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public UserGiftCardModel GetModel(string CardNumber)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select CardNumber, Password, FestivalName, FaceValue, Balance, CouponAmount, Picture, Title, BuyTime, BuyID, RecieveID, ReceiveEmail, LeaveWords  ");
			stringBuilder.Append("  from MBF_UserGiftCard ");
			stringBuilder.Append(" where CardNumber=@CardNumber ");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@CardNumber", SqlDbType.VarChar, 50)
			};
			array[0].Value = CardNumber;
			UserGiftCardModel userGiftCardModel = new UserGiftCardModel();
			DataSet dataSet = Conn.Query(stringBuilder.ToString(), array);
			UserGiftCardModel result;
			if (dataSet.Tables[0].Rows.Count > 0)
			{
				userGiftCardModel.CardNumber = dataSet.Tables[0].Rows[0]["CardNumber"].ToString();
				userGiftCardModel.Password = dataSet.Tables[0].Rows[0]["Password"].ToString();
				userGiftCardModel.FestivalName = dataSet.Tables[0].Rows[0]["FestivalName"].ToString();
				if (dataSet.Tables[0].Rows[0]["FaceValue"] != null && dataSet.Tables[0].Rows[0]["FaceValue"].ToString() != "")
				{
					userGiftCardModel.FaceValue = new decimal?(decimal.Parse(dataSet.Tables[0].Rows[0]["FaceValue"].ToString()));
				}
				if (dataSet.Tables[0].Rows[0]["Balance"] != null && dataSet.Tables[0].Rows[0]["Balance"].ToString() != "")
				{
					userGiftCardModel.Balance = new decimal?(decimal.Parse(dataSet.Tables[0].Rows[0]["Balance"].ToString()));
				}
				if (dataSet.Tables[0].Rows[0]["CouponAmount"] != null && dataSet.Tables[0].Rows[0]["CouponAmount"].ToString() != "")
				{
					userGiftCardModel.CouponAmount = new decimal?(decimal.Parse(dataSet.Tables[0].Rows[0]["CouponAmount"].ToString()));
				}
				userGiftCardModel.Picture = dataSet.Tables[0].Rows[0]["Picture"].ToString();
				userGiftCardModel.Title = dataSet.Tables[0].Rows[0]["Title"].ToString();
				if (dataSet.Tables[0].Rows[0]["BuyTime"] != null && dataSet.Tables[0].Rows[0]["BuyTime"].ToString() != "")
				{
					userGiftCardModel.BuyTime = new System.DateTime?(System.DateTime.Parse(dataSet.Tables[0].Rows[0]["BuyTime"].ToString()));
				}
				if (dataSet.Tables[0].Rows[0]["BuyID"] != null && dataSet.Tables[0].Rows[0]["BuyID"].ToString() != "")
				{
					userGiftCardModel.BuyID = new int?(int.Parse(dataSet.Tables[0].Rows[0]["BuyID"].ToString()));
				}
				if (dataSet.Tables[0].Rows[0]["RecieveID"] != null && dataSet.Tables[0].Rows[0]["RecieveID"].ToString() != "")
				{
					userGiftCardModel.RecieveID = new int?(int.Parse(dataSet.Tables[0].Rows[0]["RecieveID"].ToString()));
				}
				userGiftCardModel.ReceiveEmail = dataSet.Tables[0].Rows[0]["ReceiveEmail"].ToString();
				userGiftCardModel.LeaveWords = dataSet.Tables[0].Rows[0]["LeaveWords"].ToString();
				result = userGiftCardModel;
			}
			else
			{
				result = null;
			}
			return result;
		}
		public DataSet GetList(string strWhere)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select * ");
			stringBuilder.Append(" FROM MBF_UserGiftCard ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public DataSet GetList(int Top, string strWhere, string fieldOrder)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select ");
			if (Top > 0)
			{
				stringBuilder.Append(" top " + Top.ToString());
			}
			stringBuilder.Append(" * ");
			stringBuilder.Append(" FROM MBF_UserGiftCard ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			stringBuilder.Append(" order by " + fieldOrder);
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public DataSet GetListRowid(int Top, string strWhere, string fieldOrder, params string[] selectField)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select ");
			if (Top > 0)
			{
				stringBuilder.Append(" top " + Top.ToString());
			}
			string text = "*";
			if (selectField.Length > 0)
			{
				text = selectField[0];
				if (text.Trim().Trim(new char[]
				{
					','
				}) == "")
				{
					text = "*";
				}
			}
			stringBuilder.AppendFormat(" ROW_NUMBER() OVER (ORDER BY CardNumber ASC) rowid,{0} ", text);
			stringBuilder.Append(" FROM MBF_UserGiftCard ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			stringBuilder.Append(" order by " + fieldOrder);
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
	}
}
